package com.jqing.lib.tools;

import com.jqing.lib.print.Print;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by chshqiang on 15-6-14.
 * 是否为空
 */
public class TextTools {
    public static boolean isEmpty(CharSequence... args) {
        for (CharSequence str : args) {
            if (str != null && str.length() > 0)
                return false;
        }

        return true;
    }

    public static boolean hasEmpty(CharSequence... args) {
        for (CharSequence str : args) {
            if (str == null || str.length() == 0)
                return true;
        }

        return false;
    }

    public static boolean isEmail(String email) {
        return !isEmpty(email) && email.matches("^[a-z'0-9]+([._-][a-z'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$");
    }

    public static String md5(String str) {
        try {
            byte[] buf = str.getBytes();
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(buf);
            byte[] tmp = md5.digest();
            StringBuilder sb = new StringBuilder();
            for (byte b : tmp) {
                sb.append(Integer.toHexString(b & 0xff));
            }

            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return str;
    }


    // 校验Tag Alias 只能是数字,英文字母和中文
    public static boolean isValidTagAndAlias(String s) {
        Pattern p = Pattern.compile("^[\u4E00-\u9FA50-9a-zA-Z_-]{0,}$");
        Matcher m = p.matcher(s);
        return m.matches();
    }

    public static boolean isPhone(String phoneNumber) {
        // ^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$
        // ^[1]([3][0-9]{1}|59|58|88|89)[0-9]{8}$
        return !isEmpty(phoneNumber) && phoneNumber.matches("^[1]([3][0-9]{1}|50|56|59|58|88|89)[0-9]{8}$");
    }
    public static boolean isPassword(String password){
        //^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$
        return !isEmpty(password)&&password.matches("^[0-9A-Za-z]{6,20}$");
    }
    public static boolean isIdentityCardType(String crednum){
        return !isEmpty(crednum)&&crednum.matches("^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$");
    }
    public static boolean isOfficerType(String crednum){
        return !isEmpty(crednum)&&crednum.matches("");
    }
    public static boolean isPassportType(String crednum){
        return !isEmpty(crednum)&&crednum.matches("^(P\\d{7}|G\\d{8}|S\\d{7,8}|D\\d+|1[4,5]\\d{7})$");
    }
    public static boolean isDrivingLicenseType(String crednum){
        return !isEmpty(crednum)&&crednum.matches("");
    }
    public static boolean ischineseCharacter(String s){
        return !isEmpty(s)&&s.matches("([\\u4e00-\\u9fa5]{2,12})");
    }
    public static boolean isEnglish(String s){
        return !isEmpty(s)&&s.matches("^[a-zA-Z]{3,200}$");
    }

    public static boolean hasTailEnter(String str) {
        if (TextTools.isEmpty(str)) {
            return false;
        }

        int end = str.length();
        String tail = str.substring(end - 1, end);

        if ("\n".equals(tail)) {
            Print.m("n");
            return true;
        } else if ("\r".equals(tail)) {
            Print.m("r");
            return true;
        } else if (2 <= end) {
            tail = str.substring(end - 2, end);

            if ("\r\n".equals(tail)) {
                Print.m("rn");
                return true;
            }
        }

        return false;
    }
}
